# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


import pymysql
from twisted.enterprise import adbapi
import json
from Apps.items import AppsItem
from kafka import KafkaProducer


class MysqlPipeline(object):

    def __init__(self):
        print("进入Kafka初始化方法")
        self.producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
                                      bootstrap_servers=['192.168.215.10:9092', '192.168.215.20:9092',
                                                         '192.168.215.30:9092'])
        print(self, "--------------------------")

    def process_item(self, item, spider):
        if isinstance(item, AppsItem):
            self.process_item_to_kafka(item)
        return item

    def process_item_to_kafka(self, item):
        print("==============================")
        tmp = {
            'title': item["title"],
            'volume': item["volume"],
            'downloads': item["downloads"],
            'follow': item["follow"],
            'comment': item["comment"],
            'tags': item["tags"],
            'rank_num': item["rank_num"],
            'rom': item["rom"],
            'developer': item["developer"],
            'rank_num_users': item["rank_num_users"],
            'update_time': item["update_time"],
        }

        self.producer.send('app_list', tmp)
        print(self)
        pass

